package com.nsoft.html5npub.business;

import java.io.File;

import com.nsoft.html5npub.objects.BookItem;
import com.nsoft.html5npub.utils.Constant;
import com.nsoft.html5npub.utils.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ArrayAdapter;

/**
 * <b>Book Deleter</b> thread to delete a book, and also
 * update back to the UI.
 * 
 * @param adapter can be null. if it is null, then the main
 * thread has to hand-over the UI update.
 * 
 * @author minhld
 *
 */
public class BookDeleter extends AsyncTask<Void,Integer,Void> {
	final int DEL_FROM_DB_FAILED=-100;
	final int DEL_FROM_STORAGEG_FAILED=-101;
	final int DEL_FROM_DB=0;
	final int DEL_FROM_STORAGE=30;
	final int DEL_REARRANGE_LIST=80;
	final int DEL_DONE=100;
	
	private Context context;
	private ProgressDialog progressor=null;
	private ArrayAdapter<BookItem> adapter;
	private BookItem book=null;
	private int pos;
	
	public BookDeleter(Context ctx,
			ArrayAdapter<BookItem> _adapter,
			BookItem _book,int _pos){
		this.context=ctx;
		this.progressor=new ProgressDialog(ctx);
		this.adapter=_adapter;
		this.book=_book;
		this.pos=_pos;
	}
	
	@Override
	protected void onPreExecute(){
		progressor.setCancelable(true);
		progressor.setMessage(Constant.
				booklist.BLF_FILE_DOWNLOADING);
		progressor.show();
	}
	
	@Override
	protected Void doInBackground(Void... params) {
		try{
			publishProgress(DEL_FROM_DB);
			/**
			 * 1. delete from database
			 * 	and also the related bookmarks
			 */
			boolean retDel=dbengine.deleteBook(
							book.getContentId());
			if (retDel){
				retDel=dbengine.deleteBookmarksOfBook(
							book.getContentId());
			}
			
			/**
			 * 2. delete from local storage if exists
			 */
			if (retDel){
				publishProgress(DEL_FROM_STORAGE);
				String bookPath=utils.getBookPath()+
						Constant.PATH_SPLITTER+
						book.getContentName();
				utils.delete(new File(bookPath));
			}else{
				publishProgress(DEL_FROM_DB_FAILED);
				Log.e("engine.deleteBook()",Constant.
						business.DELETE_FROMDB_FAILED);
			}
			/**
			 * 3. refresh the book list: the state of this book
			 * row will be changed from downloaded state to not-
			 * downloaded state.
			 */
			publishProgress(DEL_REARRANGE_LIST);
			
			publishProgress(DEL_DONE);
			return null;
		}catch(Exception e){
			Log.e("BookDeleter.doInBackground()",
					e.getClass()+": "+e.getMessage());
		}
		return null;
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		if (values[0]==DEL_FROM_DB){
			progressor.setMessage(Constant.
					business.INFO_DELETE_FROMDB);
			return;
		}else if (values[0]==DEL_FROM_DB_FAILED){
			utils.showError(this.context,
					Constant.DIALOG_ERROR,
					Constant.business.
						DELETE_FROMDB_FAILED);
		}else if (values[0]==DEL_FROM_STORAGE){
			progressor.setMessage(Constant.
					business.INFO_DELETE_FROM_STORAGE);
			return;
		}else if (values[0]==DEL_REARRANGE_LIST){
			progressor.setMessage(Constant.
					business.INFO_REARRANGE_LIST);
			
			refreshBook(book,this.pos);

			return;
		}else if (values[0]==DEL_DONE){
			
		}
		progressor.dismiss();
	}
	
	private void refreshBook(BookItem book,int pos){
		BookItem newBook=book;
		newBook.setDownloaded(Boolean.FALSE.
				toString().toLowerCase());
		if (this.adapter!=null){
			this.adapter.remove(book);
			this.adapter.insert(newBook,pos);
			this.adapter.notifyDataSetChanged();
		}
	}
}
